import { Request, Response, NextFunction } from "express";
import { connection } from "@/services/database";
import { checkIsUser, generateInsertFields } from "@/utils/sql";

interface Params {
	user_id: number;
	nickname: string;
}
export default async (request: Request, response: Response, next: NextFunction) => {
	try {
		const params = request.body as unknown as Params;
		if (!checkIsUser(params.user_id)) throw new Error("未查询到用户");
		(await connection.execute(
			"INSERT INTO agency " +
				generateInsertFields({
					user_id: params.user_id,
					nickname: params.nickname,
				}),
			{ ...params }
		)) as any;
		response.json({ data: null, message: "成功" });
	} catch (error) {
		response.statusCode = 400;
		next(error);
	}
};
